/**
 * ZhiZun.com Inc.
 * Copyright (c) 2004-2010 All Rights Reserved.
 */
package com.zhz.lib.cms.utils.zhz;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.zhz.project.common.util.hibernate.BaseDAO;

/**
 * 反射工具类
 * @author liwei2672
 * @version $Id: ReflectionUtil.java, v 0.1 2010-7-17 下午02:53:34 liwei2672 Exp $
 */
public class ZhzReflectionUtil {
    private static Logger logger = Logger.getLogger(ZhzReflectionUtil.class);

    /**
     *对选择的记录进行审核，通过反射实现，在事务中进行 <br/>
     *要求对象具有 setAproveState setAproveTime setEditor方法<br/>
     * 其中 <br/>
     * setAproveState用来设置审核值<br/>
     * setAproveTime用来设置审核时间<br/>
     * setEditor用来设置审核操作员<br/>
     * 
     * @param request
     * @param transactionTemplate
     * @param baseDao
     * @param table 表名
     * @param key   字段名称
     */
    @SuppressWarnings("unchecked")
    public static void approveRecord(HttpServletRequest request,
                                     TransactionTemplate transactionTemplate,
                                     final BaseDAO baseDao, final String table, final String key) {

        // 0 未审核 1 通过 2 不通过
        String aproveState = ZhzHttpRequestUtil.getParameters(request, "approveState");

        final int apoveStateInt = Integer.parseInt(aproveState);
        final Set<String> idSet =ZhzHttpRequestUtil.getSelectedIdSet(request);
        //在事务中进行
        boolean approveResult = (Boolean) transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus status) {

                try {
                    Iterator it = idSet.iterator();
                    while (it.hasNext()) {
                        String value = (String) it.next();
                        Object record = baseDao.findOneObject(table, key, value);
                        Class ownerClass = record.getClass();

                        Method setAproveState = ownerClass.getMethod("setAproveState",
                            Integer.class);
                        Method setAproveTime = ownerClass.getMethod("setAproveTime", Date.class);
                        Method setEditor = ownerClass.getMethod("setEditor", String.class);
                        Method setEditorTime = ownerClass.getMethod("setEditorTime", Date.class);

                        setAproveState.invoke(record, apoveStateInt);
                        setAproveTime.invoke(record, new Date());
                        setEditor.invoke(record, ZhzSecurityUtil.getOperatorName());
                        setEditorTime.invoke(record, new Date());

                        baseDao.update(record);
                    }
                } catch (Exception e) {
                    logger.error("审核记录时发生异常", e);
                    status.setRollbackOnly();
                    return false;
                }
                return true;
            }
        });

        if (approveResult) {
            request.setAttribute("approveResult", "approveResult");
            request.setAttribute("app", "app");
        }
    }

}
